<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="900px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="128px"
      :model="formData"
      :rules="rules"
      size="medium"
    >
      <el-form-item label="标本号" prop="apply_num">
        <el-input v-model="formData.apply_num" clearable placeholder="标本号" />
      </el-form-item>
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="血型检验" name="first">
          <el-row>
            <el-col :span="12">
              <el-form-item label="ABO" prop="blood_group">
                <el-select
                  v-model="formData.blood_group"
                  clearable
                  filterable
                  placeholder="ABO"
                >
                  <el-option
                    v-for="(item, index) in bloodGroups"
                    :key="index"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="Rh(D)" prop="blood_rhd">
                <el-select
                  v-model="formData.blood_rhd"
                  clearable
                  filterable
                  placeholder="Rh(D)"
                >
                  <el-option
                    v-for="(item, index) in bloodRhds"
                    :key="index"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="不规则抗体筛查" prop="blood_sop">
                <el-select
                  v-model="formData.blood_sop"
                  clearable
                  filterable
                  placeholder="不规则抗体筛查"
                >
                  <el-option
                    v-for="(item, index) in bloodRhds"
                    :key="index"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="直抗" prop="dat">
                <el-select
                  v-model="formData.dat"
                  clearable
                  filterable
                  placeholder="直抗"
                >
                  <el-option
                    v-for="(item, index) in bloodResults"
                    :key="index"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="检验结果上传时间" prop="descri">
            <el-date-picker
              v-model="formData.blood_abo_time"
              placeholder="选择日期时间"
              type="datetime"
              value-format="yyyy-MM-dd hh:mm:ss"
            />
          </el-form-item>
          <el-form-item label="备注" prop="descri">
            <el-input
              v-model="formData.blood_abo_descri"
              clearable
              placeholder="备注"
              rows="3"
              type="textarea"
            />
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="Rh分型" name="second">
          <el-form-item label="Rh分型" prop="blood_rh_fx">
            <el-select
              v-model="formData.blood_rh_fx"
              clearable
              filterable
              placeholder="Rh分型"
            >
              <el-option
                v-for="(item, index) in bloodRhFxs"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="检验结果上传时间" prop="descri">
            <el-date-picker
              v-model="formData.blood_rh_fx_time"
              align="center"
              placeholder="选择日期时间"
              type="datetime"
              value-format="yyyy-MM-dd hh:mm:ss"
            />
          </el-form-item>
          <el-form-item label="备注" prop="descri">
            <el-input
              v-model="formData.blood_rh_fx_descri"
              clearable
              placeholder="备注"
              rows="3"
              type="textarea"
            />
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="血小板抗筛" name="third">
          <el-form-item label="血小板抗筛" prop="blood_xxbks">
            <el-select
              v-model="formData.blood_xxbks"
              clearable
              filterable
              placeholder="血小板抗筛"
            >
              <el-option
                v-for="(item, index) in bloodResults"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="检验结果上传时间" prop="descri">
            <el-date-picker
              v-model="formData.blood_xxbks_time"
              placeholder="选择日期时间"
              type="datetime"
              value-format="yyyy-MM-dd hh:mm:ss"
            />
          </el-form-item>
          <el-form-item label="备注" prop="descri">
            <el-input
              v-model="formData.blood_xxbks_descri"
              clearable
              placeholder="备注"
              rows="3"
              type="textarea"
            />
          </el-form-item>
        </el-tab-pane>
      </el-tabs>
      <el-form-item>
        <el-button type="primary" @click="submit('mainForm')">提交</el-button>
        <el-button @click="reset('mainForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
  import { updateOne as doEdit, addOne as doAdd } from '@/api/bloodSpecimen'
  import {
    bloodGroups,
    bloodRhds,
    bloodRhFxs,
    bloodResults,
  } from '@/utils/constant'
  import { removeEmptyField } from '@/utils'
  export default {
    data() {
      return {
        formData: {
          apply_num: '',
          blood_group: '',
          blood_rhd: '',
          blood_sop: '',
          blood_abo_time: '',
          blood_abo_descri: '',
          blood_xxbks: '',
          blood_xxbks_time: '',
          blood_xxbks_descri: '',
          blood_rh_fx: '',
          blood_rh_fx_time: '',
          blood_rh_fx_descri: '',
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        activeName: 'first',
        bloodGroups,
        bloodRhds,
        bloodRhFxs,
        bloodResults,
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.formData = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.formData = {
          apply_num: '',
          blood_group: '',
          blood_rhd: '',
          blood_sop: '',
          blood_abo_time: '',
          blood_abo_descri: '',
          blood_xxbks: '',
          blood_xxbks_time: '',
          blood_xxbks_descri: '',
          blood_rh_fx: '',
          blood_rh_fx_time: '',
          blood_rh_fx_descri: '',
        }
        this.dialogFormVisible = false
      },
      submit() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.title === '编辑') {
              let { msg } = await doEdit(
                this.formData.apply_num,
                removeEmptyField(this.formData)
              )
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            } else {
              let { msg } = await doAdd(removeEmptyField(this.formData))
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
